package com.palnar.ilecker.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.ksoap2.serialization.SoapObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.palnar.ilecker.utils.XmlDocumentUtility;

import android.util.Base64;

public class MenuItem extends Item implements Serializable {
	// private int id;
		private Group category;
		// private String name;
		private List<Ingredient> ingredients = new ArrayList<Ingredient>();
		private List<SpecificationGroup> specificationGroups = new ArrayList<SpecificationGroup>();
		private String description;
		private double price;
		private HashMap<Integer, Double> pricesAgainstMenu = new HashMap<Integer, Double>();
		private byte[] image;
		private String itemCode;
		private Boolean itemAvailable;
		private static String idArg = "Id";
		private static String nameArg = "Name";
		private static String codeArg = "ItemCode";

		public MenuItem() {
			// TODO Auto-generated constructor stub
		
		}

		public List<Ingredient> getIngredients() {
			// TODO Auto-generated method stub
			return this.ingredients;
		}

//		public String getDescription() {
//			// TODO Auto-generated method stub
//			return this.description;
//		}

		public double GetPrice() {
			// TODO Auto-generated method stub
			return this.price;
		}

		public Group getCategory() {
			// TODO Auto-generated method stub
			return this.category;
		}

		public byte[] getImage() {
			// TODO Auto-generated method stub
			return this.image;
		}

		public Boolean getIsAvailable() {
			// TODO Auto-generated method stub
			return this.itemAvailable;
		}
		
		public List<SpecificationGroup> getSpecificationGroups() {
			return this.specificationGroups;
		}
		
		public String getCode() 
		{	
			return this.itemCode;
		}

//		public Map<Integer, String> getDisplayNames()
//		{
//		  return this.displayNames;
//		}
//		
//		public Map<Integer, String> getDisplayDescriptions()
//		{
//		  return this.displayDescription;
//		}

		public boolean ContainsIngredient(String ingredientName) {
			// TODO Auto-generated method stub
			return false;
		}

		public void RemoveIngredient(Ingredient ingredient) {
			// TODO Auto-generated method stub
			this.ingredients.remove(0);
		}

		public void setIngredients(List<Ingredient> ingredients) {
			// TODO Auto-generated method stub
			for (Ingredient ingredient : ingredients) {
				this.ingredients.add(ingredient);
			}
		}

//		public void setDescription(String description) {
//			// TODO Auto-generated method stub
//			this.description = description;
//		}

		public void SetPrice(double price)
		{
			// TODO Auto-generated method stub
			this.price = price;
		}

		public void setCategory(Group category) {
			// TODO Auto-generated method stub
			this.category = category;
		}

		public void setImage(byte[] image) {
			// TODO Auto-generated method stub
			this.image = image;
		}
		
		public void setCode(String itemCode) {
			// TODO Auto-generated method stub
			this.itemCode = itemCode;
		}
		
		public void setIsAvailable(Boolean itemAvailable) {
			this.itemAvailable = itemAvailable;
		}
		
//		public void setDisplayNames(Map<Integer, String> displayNames)
//		{
//		  this.displayNames= displayNames;
//		}
//		
//		public void setDisplayDescriptions(Map<Integer, String> displayDescription)
//		{
//		  this.displayDescription= displayDescription;
//		}

		public void SetSpecificationGroups(List<SpecificationGroup> specGroups) {
			for (SpecificationGroup specificationGroup : specGroups) {
				this.specificationGroups.add(specificationGroup);
			}
		}
		
		public Ingredient TryGetIngredient(Integer ingredientId) {
			if (!this.ingredients.isEmpty()) {
				for (Ingredient ingredient : this.ingredients) {
					if (ingredient.getClientId() == ingredientId) {
						return ingredient;
					}
				}
			}
			return null;
		}

		public SpecificationGroup TryGetSpecificationGroups(Integer specGroupId) {
			if (!this.specificationGroups.isEmpty()) {
				for (SpecificationGroup specGroup : this.specificationGroups) {
					if (specGroup.getClientId() == specGroupId) {
						return specGroup;
					}
				}
			}
			return null;
		}

		public void getSerializationData(Element parentElement, Document document)
		{
			Element idElement = document.createElement(idArg);
			Node idNode = document.createTextNode("id");
			idNode.setNodeValue(getServerId().toString());
			idElement.appendChild(idNode);
			parentElement.appendChild(idElement);

			Element nameElement = document.createElement(nameArg);
			Node nameNode = document.createTextNode("name");
			nameNode.setNodeValue(getServerNameId().toString());
			nameElement.appendChild(nameNode);
			parentElement.appendChild(nameElement);

			Element codeElement = document.createElement(codeArg);
			Node codeNode = document.createTextNode("ItemCode");
			codeNode.setNodeValue(itemCode.toString());
			codeElement.appendChild(codeNode);
			parentElement.appendChild(codeElement);

		}

		public static MenuItem MenuItemGroupBuilder(SoapObject outputSoapObject, HashMap<Integer, Language> languages)
		{
			// SoapObject specificationGroupSoapObject;
			MenuItem menuItem = new MenuItem();
			List<Ingredient> ingredientList = new ArrayList<Ingredient>();
			SoapObject ingredientGroupSoapObject = (SoapObject) outputSoapObject.getProperty("Ingredients");

			SoapObject specificationGroupsSoapObject = (SoapObject) outputSoapObject.getProperty("SpecificationGroups");

			List<SpecificationGroup> specificationGroupList = new ArrayList<SpecificationGroup>();

			for (int i = 0; i < specificationGroupsSoapObject.getPropertyCount(); i++)
			{
				SpecificationGroup specificationGroup = new SpecificationGroup(null);
				SoapObject specGroupSoapObject = (SoapObject)specificationGroupsSoapObject.getProperty(i);
				specificationGroup = SpecificationGroup.SpecificationGroupBuilder(specGroupSoapObject, languages);
				specificationGroupList.add(specificationGroup);
			}

			menuItem.specificationGroups = specificationGroupList;

			System.out.println(specificationGroupsSoapObject.toString());

			menuItem.setServerId(Integer.parseInt(outputSoapObject.getProperty("Id").toString()));
			if (outputSoapObject.getProperty("Name") != null)
			{
				if(!outputSoapObject.getProperty("Name").toString().equals(XmlDocumentUtility.nullString))
				{
					menuItem.setServerNameId(Integer.parseInt(outputSoapObject.getProperty("Name").toString()));
				}
			}
			if(outputSoapObject.getProperty("Description") != null)
			{
				if(!outputSoapObject.getProperty("Description").toString().equals(XmlDocumentUtility.nullString))
				{
				    menuItem.setDescription(Integer.parseInt(outputSoapObject.getProperty("Description").toString()));
				}
			}
			if(outputSoapObject.getProperty("ItemPrice")!= null)
			{
				if(!outputSoapObject.getProperty("ItemPrice").toString().equals(XmlDocumentUtility.nullString))
				{
				    menuItem.SetPrice(Double.parseDouble(outputSoapObject.getProperty("ItemPrice").toString()));
				}
			}
			//menuItem.SetPrice(Double.parseDouble(outputSoapObject.getProperty("ItemPrice").toString()));
			menuItem.setIsAvailable(Boolean.parseBoolean(outputSoapObject.getProperty("IsAvaiable").toString()));
			String imageArray;
			if (outputSoapObject.getProperty("ItemImage") != null)
			{
				imageArray = outputSoapObject.getProperty("ItemImage").toString();
				byte[] decodedString = null;
				decodedString = Base64.decode(imageArray, Base64.DEFAULT);

				// Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString,
				// 0, decodedString.length);

				menuItem.setImage(decodedString);
			}
			
			if(outputSoapObject.getProperty("ItemCode")!= null)
			{
				menuItem.itemCode = outputSoapObject.getProperty("ItemCode").toString();
			}
			
			

			for (int i = 0; i < ingredientGroupSoapObject.getPropertyCount(); i++)
			{
				Ingredient ingredient = new Ingredient();
				SoapObject itemGroupSoapObject2 = (SoapObject) ingredientGroupSoapObject.getProperty(i);
				ingredient = Ingredient.IngredientBuilder(itemGroupSoapObject2, languages);
				ingredientList.add(ingredient);
			}

			menuItem.setIngredients(ingredientList);
			return menuItem;
		}

		private HashMap<Integer, Double> getPricesAgainstMenu()
		{
			return pricesAgainstMenu;
		}

		private void setPricesAgainstMenu(HashMap<Integer, Double> pricesAgainstMenu)
		{
			this.pricesAgainstMenu = pricesAgainstMenu;
		}
}
